//
//  YMTranslacteResult.m
//  YueYue
//
//  Created by LiuYu on 2024/10/24.
//

#import "YMTranslacteRequest.h"

@implementation YMTranslacteResult

@end

@implementation YMTranslacteRequest
{
    YMTranslacteRequestType _type;
}

- (instancetype)initWithType:(YMTranslacteRequestType)type
{
    self = [super init];
    if (self) {
        _type = type;
    }
    return self;
}

- (NSString *)requestUrl
{
    switch (_type) {
        case YMTranslacteRequestTypeTranslacte:
            return @"/app/tran/text1";
        case YMTranslacteRequestTypeSaveSelfLang:
            return @"/user/saveUserLang";
        case YMTranslacteRequestTypeGetUserLang:
            return @"/user/getUserLang";
        case YMTranslacteRequestTypeGetTimeout:
            return @"/app/tran/timeout";
        default:
            return @"";
    }
}

- (id)requestArgument
{
    return _params?:@{};
}

- (NSTimeInterval)requestTimeoutInterval
{
    if (_type == YMTranslacteRequestTypeTranslacte) {
        NSString *tranSource = _params[@"tranSource"];
        if ([tranSource isEqualToString:@"2"]) {
            // 讯飞
            NSNumber *number = HLHelper.shareInstance.cache[@"GPTranslactionTimeoutKey"];
            if (!number) {
                return 1;
            }
            return number.floatValue / 1000;
        } else {
            // 小牛
            return 60; // 这个地方不要使用[super requestTimeoutInterval]
        }
        
    }
    return [super requestTimeoutInterval];
}

- (YTKRequestMethod)requestMethod
{
    switch (_type) {
        case YMTranslacteRequestTypeTranslacte:
            return YTKRequestMethodPOST;
        case YMTranslacteRequestTypeSaveSelfLang:
            return YTKRequestMethodGET;
        case YMTranslacteRequestTypeGetUserLang:
            return YTKRequestMethodGET;
        case YMTranslacteRequestTypeGetTimeout:
            return YTKRequestMethodPOST;
        default:
            return YTKRequestMethodGET;
    }
}

- (void)requestFailedFilter
{
    if (_type == YMTranslacteRequestTypeTranslacte) {
        NSString *tranSource = _params[@"tranSource"];
        if ([tranSource isEqualToString:@"2"]) {
            return;
        }
    }
    
    [super requestFailedFilter];
}

- (void)requestCompleteFilter
{
    if (_type == YMTranslacteRequestTypeTranslacte) {
        NSString *tranSource = _params[@"tranSource"];
        if ([tranSource isEqualToString:@"2"]) {
            self.result = self.responseJSONObject;
            return;
        }
    }
    
    [super requestCompleteFilter];
}

- (NSString *)tranlacteResult
{
    NSString *translacte = [self.result valueForKey:@"data"];

    if (![translacte isKindOfClass:[NSString class]]){
        return translacte;
    }

    return translacte;
}

- (void)startWithCompletionBlockWithSuccess:(YTKRequestCompletionBlock)success failure:(YTKRequestCompletionBlock)failure
{
    if (_type == YMTranslacteRequestTypeTranslacte) {
        NSMutableDictionary *tempParams = [NSMutableDictionary dictionaryWithDictionary:_params];
        NSString *tranSource = _params[@"tranSource"];
        if (tranSource == nil) {
            // 第一次请求：讯飞
            tempParams[@"tranSource"] = @"2";
            _params = tempParams;
            
            [super startWithCompletionBlockWithSuccess:^(__kindof HLBaseRequest * _Nonnull request) {
                if (request.isSuccess) {
                    success(request);
                } else {
                    
                    NSMutableDictionary *p = [NSMutableDictionary dictionaryWithDictionary:self.params];
                    p[@"tranSource"] = @"1";
                    
                    YMTranslacteRequest *xiaoNiuRequest = [YMTranslacteRequest.alloc initWithType:YMTranslacteRequestTypeTranslacte];
                    xiaoNiuRequest.params = p;
                    [xiaoNiuRequest startWithCompletionBlockWithSuccess:success failure:failure];
                }
            } failure:^(__kindof YTKBaseRequest * _Nonnull request) {
                // 如果请求失败，自动进行下一轮请求，
                NSMutableDictionary *p = [NSMutableDictionary dictionaryWithDictionary:self.params];
                p[@"tranSource"] = @"1";
                
                YMTranslacteRequest *xiaoNiuRequest = [YMTranslacteRequest.alloc initWithType:YMTranslacteRequestTypeTranslacte];
                xiaoNiuRequest.params = p;
                [xiaoNiuRequest startWithCompletionBlockWithSuccess:success failure:failure];
            }];
        } else {
            [super startWithCompletionBlockWithSuccess:success failure:failure];
        }
        
    } else if (_type == YMTranslacteRequestTypeGetTimeout) {
        [super startWithCompletionBlockWithSuccess:success failure:failure];
    } else {
        [super startWithCompletionBlockWithSuccess:success failure:failure];
    }
}

@end
